//============================================================================
// Name        : screen.cpp
// Author      : dfoelber
//============================================================================

#include "graphics/screen.h"
#include "SDL/SDL_ttf.h"
#include <iostream>

using namespace std;

/**
* Constructor
**/
Screen::Screen()
{
}

/**
* Destructor
**/
Screen::~Screen()
{
	//Quit font system
	TTF_Quit();
	
    //Quit SDL
    SDL_Quit();
}

/**
* Shows the screen, initializes surfaces, and centers the screen.
* 
* @param width screen width
* @param height screen height
* @param depth screen color depth
* @return success
**/
bool Screen::Show( int width, int height, int depth )
{
	//These environment variables will center the screen
	putenv("SDL_VIDEO_WINDOW_POS");
	putenv("SDL_VIDEO_CENTERED=1");
	getenv("SDL_VIDEO_WINDOW_POS");
	getenv("SDL_VIDEO_CENTERED");
	
	//Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;    
    }
    
    //Initialize TTF font system
    if( TTF_Init() == -1 ) 
    { 
    	return false; 
    }
    
    //Set up the screen
    SDL_Surface* screen = SDL_SetVideoMode( width, height, depth, SDL_SWSURFACE | SDL_DOUBLEBUF );
    
    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;    
    }    

    surface = new Surface( screen );
    
    //Set the window caption
    SDL_WM_SetCaption( "Xyne", NULL );
    
    return true;
}

/**
* Gets the screen's surface
* 
* @return screen's surface
**/
Surface* Screen::GetSurface()
{
	return surface;
}

/**
* Toggles between full screen and windowed mode.
**/
void Screen::ToggleFullScreen()
{
	if( surface->GetSurface() != NULL )
	{
		SDL_WM_ToggleFullScreen( surface->GetSurface() );
	}
}

/**
* Performs a full screen update (slow).
**/
void Screen::Update()
{
	SDL_Flip( surface->GetSurface() );
}
